
#include "Core.h"

namespace p2 {

Timer::Timer(bool createStarted)
	: _stopped(0)
{
#ifdef _WIN32
	QueryPerformanceFrequency(&_frequency);
	_startCount.QuadPart = 0;
	_endCount.QuadPart = 0;
#else
	_startCount.tv_sec = _startCount.tv_usec = 0;
	_endCount.tv_sec = _endCount.tv_usec = 0;
#endif
	if (createStarted) {
		start();
	}
}

Timer::~Timer()
{
}

void Timer::start()
{
#ifdef _WIN32
	QueryPerformanceCounter(&_startCount);
#else
	gettimeofday(&_startCount, NULL);
#endif
	_stopped = false;
}

void Timer::stop()
{
#ifdef _WIN32
	QueryPerformanceCounter(&_endCount);
#else
	gettimeofday(&_endCount, NULL);
#endif
	_stopped = true;
}

real64 Timer::getElapsedTimeInMicroSec()
{
#ifdef _WIN32
	if (!_stopped) {
		QueryPerformanceCounter(&_endCount);
	}
	return (real64) (_endCount.QuadPart - _startCount.QuadPart);
#else
	if (!_stopped)
	{
		gettimeofday(&_endCount, NULL);
	}
	return ((_endCount.tv_sec * 1000000.0) + _endCount.tv_usec) -
	((_startCount.tv_sec * 1000000.0) + _startCount.tv_usec);
#endif
}

real64 Timer::getElapsedTimeInMilliSec()
{
	return getElapsedTimeInMicroSec() * 0.001;
}

real64 Timer::getElapsedTimeInSec()
{
	return getElapsedTimeInMicroSec() * 0.000001;
}

}
